<?php

namespace App\Console\Commands;

use App\Jobs\SendChangeOrderMessage;
use App\Models\Goods;
use App\Models\GoodsSetmeal;
use App\Models\GoodsSpec;
use App\Models\MemberOther;
use App\Models\Order;
use App\Models\OrderGoods;
use App\Models\OrderLease;
use App\Models\OrderRemark;
use App\Models\OtherOrder;
use App\Models\OtherOrderAllocate;
use App\Models\OtherWithholdLog;
use App\Models\SmsRecord;
use App\Models\SmsTemplate;
use App\Services\AlipayMessageService;
use App\Services\AlipayService;
use App\Services\ChargeService;
use App\Services\EsignService;
use App\Services\OrderService;
use App\Services\WechatService;
use Illuminate\Console\Command;

class WaitPayOrderCommand extends BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'WaitPayOrder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '自动发送待支付订单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    protected function handleExec()
    {
        $this->cancelOrder();
    }


    /**
     * 签约未支付自动取消订单
     */
    public function cancelOrder()
    {
        try{
            $data = Order::from('sp_order_2 as o')
                ->select('o.id')
                ->where('o.status',3)
                ->where('o.sign_status',2)
                ->where('o.withhold_status',1)
                ->orderBy('o.id', 'asc')
                ->where('o.create_time','>=',strtotime("-3 hours"))
                ->select('o.create_time','o.id')
                ->get();
            $data = obToArray($data);
            if($data){
                foreach ($data as $item){
                    SendChangeOrderMessage::dispatch($item['id'],AlipayMessageService::KX_SHOPPING);
                }
                $this->infoLog('待支付消息发送执行成功',$data);
            }
        }catch (\Exception $exception){
            $this->infoLog('待支付消息发送执行失败'.$exception->getMessage());
        }

        try{
            //数据
            $data = Order::from('sp_order_2 as o')
                ->select('o.id')
                ->where('o.status',3)
                ->where('o.sign_status',2)
                ->where('o.withhold_status',1)
                ->orderBy('o.id', 'asc')
                ->where('o.create_time','>=',strtotime("-1 days"))
                ->select('o.create_time','o.id')
                ->get();
            $data = obToArray($data);
            if($data){
                foreach ($data as $item){
                    SendChangeOrderMessage::dispatch($item['id'],AlipayMessageService::KX_SHOPPING);
                }
                $this->infoLog('待支付消息发送执行成功',$data);
            }
        }catch (\Exception $exception){
            $this->infoLog('待支付消息发送执行失败'.$exception->getMessage());
        }

    }
}
